package com.sx.zongzhi.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.HouseBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.fragment.HouseBasicInforFragment;
import com.sx.zongzhi.fragment.HouseRenterFragment;
import com.sx.zongzhi.presenter.FactHouseDetailPest;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IFactHouseDetailView;
import com.sx.zongzhi.widget.CustomDialog;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定房屋详情
 */
public class BuildingHouseDetailActivity extends BaseActivity implements IFactHouseDetailView {

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments=null;
    private HouseBasicInforFragment houseBasicInforFragment;
    private HouseRenterFragment houseRenterFragment;
    /**房屋id */
    private String houseId;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        setTitleName("房屋详情");
        setTitleRight("解绑");
        ButterKnife.bind(this);
        houseId = getIntent().getStringExtra("HouseId");
        LoadDialog.show(BuildingHouseDetailActivity.this,"加载中...");
        new FactHouseDetailPest(this).getFactHouseDetail(this,houseId);
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                createDelete();
                break;
        }
    }

    private void initView() {
        List<String> titles=new ArrayList<>();
        titles.add("基本信息");
        titles.add("住房成员");
        fragments=new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putSerializable("houseBean",houseBean);

        houseBasicInforFragment = new HouseBasicInforFragment();
        houseBasicInforFragment.setArguments(bundle);

        houseRenterFragment = new HouseRenterFragment();
        houseRenterFragment.setArguments(bundle);

        fragments.add(houseBasicInforFragment);
        fragments.add(houseRenterFragment);
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    /**解绑*/
    private void createDelete() {
        CustomDialog.Builder builder=new CustomDialog.Builder(this);
        CustomDialog dialog=builder.setMessage("确定此房屋所绑定的楼宇吗？").
                setBackButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                LoadDialog.show(BuildingHouseDetailActivity.this,"解绑中...");
                new FactHouseDetailPest(BuildingHouseDetailActivity.this)
                        .unbindBuilding(BuildingHouseDetailActivity.this,houseId);
                dialog.dismiss();
            }
        }).create();
        dialog.show();
    }

    private HouseBean houseBean;
    @Override
    public void getFactHouseDetailSuccess(HouseBean houseBeans) {
        LoadDialog.dismiss(BuildingHouseDetailActivity.this);
        houseBean = houseBeans;
        initView();
    }

    @Override
    public void deleteFactHouseSuccess(Boolean deleteResult) {
        if (deleteResult) {
            LoadDialog.dismiss(BuildingHouseDetailActivity.this);
            ToastUtils.showToast(this,"解绑成功");
            MyRxBus.get().post(new MsgEvent<>("BuildingLayoutActivity",2,"解绑成功"));
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(BuildingHouseDetailActivity.this);
        ToastUtils.showToast(BuildingHouseDetailActivity.this,message);
    }
}
